2666. Half

 

Fill an array n × n in the next way: secondary diagonal contains zeros, all cells above it contains twos and all cells below it contains ones.

 

Input. One positive integer n (n ≤ 20).

 

Output.  Print the array filled like described above.

 

Sample input

Sample output

3

220

201

011

 

 

SOLUTION

arrays

 

Algorithm analysis

Fill a two dimensional array using a double loop as indicated in the problem statement.

The cell (i, j) lies:

·        on the secondary diagonal, if i + j = n – 1;

·        below the secondary diagonal, if i + j > n – 1;

·        above the secondary diagonal, if i + j < n – 1;

 

Algorithm realization

Declare the two dimentional array.

 

#define MAX 30

int m[MAX][MAX];

 

Read the input data. Fill the array.

 

scanf("%d",&n);

for(i = 0; i < n; i++)

for(j = 0; j < n; j++)

  if (i + j == n - 1) m[i][j] = 0; else

  if (i + j < n - 1)  m[i][j] = 2; else m[i][j] = 1;

   

Print the two dimensional array.

 

for(i = 0; i < n; i++)

{

  for(j = 0; j < n; j++)

    printf("%d",m[i][j]);

  printf("\n");

}

 

Algorithm realization – double pointer

 

#include <stdio.h>

 

int **m;

int i, j, n;

 

int main(void)

{

  scanf("%d", &n);

  m = new int* [n];

  for (i = 0; i < n; i++)

    m[i] = new int[n];

 

  for (i = 0; i < n; i++)

  for (j = 0; j < n; j++)

    if (i + j == n - 1) m[i][j] = 0; else

    if (i + j < n - 1)  m[i][j] = 2; else m[i][j] = 1;

 

 

  for (i = 0; i < n; i++)

  {

    for (j = 0; j < n; j++)

      printf("%d", m[i][j]);

    printf("\n");

  }

 

  delete[] m;

  return 0;

}

 

Algorithm realization – calloc

 

#include <stdio.h>

#include <malloc.h>

 

int **m;

int i, j, n;

 

int main(void)

{

  scanf("%d", &n);

  m = (int**)calloc(n, sizeof(int*));

  for (i = 0; i < n; i++)

    m[i] = (int*)calloc(n, sizeof(int));

 

  for (i = 0; i < n; i++)

  for (j = 0; j < n; j++)

    if (i + j == n - 1) m[i][j] = 0; else

    if (i + j < n - 1)  m[i][j] = 2; else m[i][j] = 1;

 

  for (i = 0; i < n; i++)

  {

    for (j = 0; j < n; j++)

      printf("%d", m[i][j]);

    printf("\n");

  }

 

  for (i = 0; i < n; i++)

    free(m[i]);

  free(m);

  return 0;

}

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int m[][] = new int[n][n];

   

    for(int i = 0; i < n; i++)

    for(int j = 0; j < n; j++)

      if (i + j == n - 1) m[i][j] = 0; else

      if (i + j < n - 1)  m[i][j] = 2; else m[i][j] = 1;

 

    for(int i = 0; i < n; i++)

    {

      for(int j = 0; j < n; j++)

        System.out.print(m[i][j]);

      System.out.println();

    }

    con.close();

  }

}

 

Java realization – two dimensional ArrayList

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    ArrayList<ArrayList<Integer>> m =

             new ArrayList<ArrayList<Integer>>();

 

    for(int i = 0; i < n; i++)

      m.add(new ArrayList<Integer>());

   

    for(int i = 0; i < n; i++)

    for(int j = 0; j < n; j++)

      if (i + j == n - 1) m.get(i).add(0); else

      if (i + j < n - 1)  m.get(i).add(2); else m.get(i).add(1);

           

    for(int i = 0; i < n; i++)

    {

      for(int j = 0; j < n; j++)

        System.out.print(m.get(i).get(j));

      System.out.println();

    }

    con.close();

  }

}

 

Java realization – array of ArrayList

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    ArrayList<Integer>[] m = new ArrayList[n];

    for(int i = 0; i < n; i++)

      m[i] = new ArrayList<Integer>();

   

    for(int i = 0; i < n; i++)

    for(int j = 0; j < n; j++)

      if (i + j == n - 1) m[i].add(0); else

      if (i + j < n - 1)  m[i].add(2); else m[i].add(1);

           

    for(int i = 0; i < n; i++)

    {

      for(int j = 0; j < n; j++)

        System.out.print(m[i].get(j));

      System.out.println();

    }

    con.close();

  }

}

 

Python realization

 

n = int(input())

 

def char(i, j):

  if i < j:

    return '2'

  if i == j:

    return '0'

  return '1'

 

for i in range(n):

  print (''.join(reversed([char(i,j) for j in range(n)])))

 

Python realization – build a matrix

 

n = int(input())

 

def char(i, j):

  if i + j < n - 1:

    return '2'

  elif i + j == n - 1:

    return '0'

  else: return '1'

 

a = []

for i in range(n):

  a.append(''.join([char(i,j) for j in range(n)]))

 

for i in range(n):

  print(a[i])